package errorhandler

import (
	"mall/service/backend/common/errorx"
	"mall/service/backend/common/i18n"
	"mall/service/backend/common/msg/logmsg"
	"mall/service/attachment/model"

	"github.com/zeromicro/go-zero/core/logx"
)

// DefaultRepositoryError returns errors dealing with default functions.
func DefaultRepositoryError(logger logx.Logger, err error, detail any) error {
	if err != nil {
		switch err {
		case model.ErrNotFound:
			logger.Errorw(err.Error(), logx.Field("detail", detail))
			return errorx.NewInvalidArgumentError(i18n.TargetNotFound)
		case model.IsConstraintError:
			logger.Errorw(err.Error(), logx.Field("detail", detail))
			return errorx.NewInvalidArgumentError(i18n.ConstraintError)
		case model.IsValidationError:
			logger.Errorw(err.Error(), logx.Field("detail", detail))
			return errorx.NewInvalidArgumentError(i18n.ValidationError)
		case model.IsNotSingular:
			logger.Errorw(err.Error(), logx.Field("detail", detail))
			return errorx.NewInvalidArgumentError(i18n.NotSingularError)
		default:
			logger.Errorw(logmsg.DatabaseError, logx.Field("detail", err.Error()))
			return errorx.NewInternalError(i18n.DatabaseError)
		}
	}

	return err
}
